﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using XYAC.Framework.MonitorTireMaterialOnline.Extensions;

namespace XYAC.Framework.MonitorTireMaterialOnline.SQLServerDB
{
    public class SQLServerMesContext : DbContext
    {
        public SQLServerMesContext(IOptionsMonitor<AppConfig> moduleConfig) : base()
        {
            this.ConnectionString = moduleConfig.CurrentValue?.SQLServerConnectionString;
        }

        public string ConnectionString { get; }

        public DbSet<YB_LOT_INSPECT> YB_LOT_INSPECT { get; set; }
        public DbSet<YB_PROCESS_STEP> YB_PROCESS_STEP { get; set; }
        public DbSet<YB_Technology_Header> YB_Technology_Header { get; set; }
        public DbSet<YB_Technology_Line> YB_Technology_Line { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<YB_LOT_INSPECT>(option =>
            {
                option.ToTable("YB_LOT_INSPECT");
                option.Property(h => h.LJ_ID).HasColumnName("LJ_ID");
            });

            modelBuilder.Entity<YB_Technology_Header>(option =>
            {
                option.ToTable("YB_TECHNOLOGY_HEADER");
                option.Property(h => h.LJ_ID).HasColumnName("LJ_ID");

            });
            modelBuilder.Entity<YB_Technology_Line>(option =>
            {
                option.ToTable("YB_TECHNOLOGY_LINE");
                option.Property(h => h.LJ_ID).HasColumnName("LJ_ID");

            });
            modelBuilder.Entity<YB_PROCESS_STEP>(option =>
            {
                option.ToTable("YB_PROCESS_STEP");
                option.Property(h => h.LJ_ID).HasColumnName("LJ_ID");

            });

            base.OnModelCreating(modelBuilder);
        }

        /// <summary>
        /// 当DB发生OnConfiguring用于配置数据库
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // 配置 SQL Server 数据库连接字符串
            //optionsBuilder.UseOracle(ConnectionString, option => option.UseOracleSQLCompatibility("11"));
            optionsBuilder.UseSqlServer(ConnectionString);
            //optionsBuilder.UseOracle(ConnectionString);
        }
    }
}
